package com.xz.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xz.R;
import com.xz.client.VideoClient;
import com.xz.entity.Video;
import com.xz.entity.Vplayer;
import com.xz.entity.frontvo.VideoFrontVo;
import com.xz.exceptionhandler.BaseException;
import com.xz.mapper.VideoMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xz.service.VideoService;
import com.xz.service.VplayerService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2020-08-09
 */
@Service
public class VideoServiceImpl extends ServiceImpl<VideoMapper, Video> implements VideoService {
    @Autowired
    private VideoClient videoClient;
    @Autowired
    private VplayerService vplayerService;
    @Override
    public String saveVideoInfo(Video video) {
        video.setBuyCount(0L);
        video.setVersion(0L);
        video.setViewCount(0L);
        video.setStatus("Draft");
        int insert = this.baseMapper.insert(video);
        if(insert == 0) {
            //添加失败
            throw new BaseException(2001,"添加信息失败");
        }

        String id=video.getId();
        return id;
    }

    @Override
    public boolean removeAll(String id) {
        int i = this.baseMapper.deleteById(id);
        if(i==0){
            //添加失败
            throw new BaseException(2001,"添加信息失败");
        }
        QueryWrapper<Vplayer> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("video_id",id);
        Vplayer vplayer = vplayerService.getOne(queryWrapper);
        videoClient.removeAlyVideo(vplayer.getVideoSourceId());
        vplayerService.removeById(vplayer.getId());
        return true;
    }

    @Cacheable(value = "recommend",key = "'selectRecommendList'")
    @Override
    public IPage<Video> getRecommend() {
        QueryWrapper<Video> queryWrapper=new QueryWrapper<>();
        //创建page对象
        Page<Video> page = new Page<>(1,8);
        //排序
        queryWrapper.orderByDesc("gmt_create");
        IPage<Video> pageInfo = this.baseMapper.selectPage(page, queryWrapper);
        return pageInfo;
    }


    @Override
    public IPage<Video> pageCondition(long current, long limit, VideoFrontVo videoFrontVo) {
        QueryWrapper<Video> queryWrapper=new QueryWrapper<>();
        //创建page对象
        Page<Video> page = new Page<>(current,limit);
        String title = videoFrontVo.getTitle();
        String categoryParentId = videoFrontVo.getCategoryParentId();
        String categoryId = videoFrontVo.getCategoryId();
        if(!StringUtils.isEmpty(title)) queryWrapper.eq("title",title);
        if(!StringUtils.isEmpty(categoryParentId)) queryWrapper.eq("category_id",categoryId);
        if(!StringUtils.isEmpty(categoryId)) queryWrapper.eq("category_parent_id",categoryParentId);
        queryWrapper.orderByDesc("gmt_create");
        IPage<Video> pageInfo = this.baseMapper.selectPage(page, queryWrapper);
        return pageInfo;
    }
}
